package cn.wolfcode.luowowo.acticle.service.impl;

import cn.wolfcode.luowowo.acticle.domain.Strategy;
import cn.wolfcode.luowowo.acticle.domain.StrategyCatalog;
import cn.wolfcode.luowowo.acticle.domain.StrategyDetail;
import cn.wolfcode.luowowo.acticle.mapper.StrategyCatalogMapper;
import cn.wolfcode.luowowo.acticle.mapper.StrategyMapper;
import cn.wolfcode.luowowo.acticle.query.StrategyCatalogQueryObject;
import cn.wolfcode.luowowo.acticle.service.IStrategyCatalogService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 攻略分类服务实现类
 */
@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;
    @Autowired
    private StrategyMapper strategyMapper;
    //查询所有攻略分类
    @Override
    public PageInfo query(StrategyCatalogQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyCatalog> list = strategyCatalogMapper.selectForList(qo);
        return new PageInfo(list);
    }

    //删除攻略分类
    @Override
    public void delete(Long id) {
        strategyCatalogMapper.deleteByPrimaryKey(id);
    }
    //保存/更新
    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        //保存地区id
        Long strategyId = strategyCatalog.getStrategy().getId();
        Strategy strategy = strategyMapper.selectByPrimaryKey(strategyId);
        strategyCatalog.setDestId(strategy.getDest().getId());
        if(strategyCatalog.getId() != null){
            strategyCatalogMapper.updateByPrimaryKey(strategyCatalog);
        }else{
            strategyCatalogMapper.insert(strategyCatalog);
        }
    }

    @Override
    public List<StrategyDetail> getCatalogByStrategyId(Long strategyId) {
        return strategyCatalogMapper.selectCatalogByStrategyId(strategyId);
    }

    @Override
    public List<StrategyCatalog> getCatalogByDestId(Long destId) {
        return strategyCatalogMapper.selectCatalogByDestId(destId);
    }

    @Override
    public StrategyCatalog getCatalogByCatalogId(Long catalogId) {
        return strategyCatalogMapper.selectByPrimaryKey(catalogId);
    }

}
